package rwlivbk.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import rwlivbk.model.Dish;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    private static final String TODAY_DISHES_KEY = "today_dishes";

    private final RedisTemplate<String, ArrayList<Dish>> redisTemplate;

    @Autowired
    public RedisService(RedisTemplate<String, ArrayList<Dish>> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public ArrayList<Dish> getTodayDishes() {
        return redisTemplate.opsForValue().get(TODAY_DISHES_KEY);
    }

    public void setTodayDishes(ArrayList<Dish> dishes) {
        // Set the dishes with an expiration time, for example, 24 hours
        redisTemplate.opsForValue().set(TODAY_DISHES_KEY, dishes, 24, TimeUnit.HOURS);
    }

    public void deleteTodayDishesCache() {
        redisTemplate.delete(TODAY_DISHES_KEY);
    }
}
